package com.gency.subscribe.web.controller.system;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gency.subscribe.core.util.http.DateUtil;
import com.gency.subscribe.model.system.Depart;
import com.gency.subscribe.model.system.PageBean;
import com.gency.subscribe.model.system.PolicyBase;
import com.gency.subscribe.model.system.PolicyStatiscesResult;
import com.gency.subscribe.service.system.IDepartService;
import com.gency.subscribe.service.system.IPolicyBaseService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马行空
 * @since 2018-06-17
 */
@Controller
@RequestMapping("/policyBase")
public class PolicyBaseController {

	
	@Autowired
	IPolicyBaseService policyBaseService;
	@Autowired
	IDepartService departService;
	
	@GetMapping("/list")
	public ModelAndView list(PageBean pb,ModelAndView mv,PolicyBase base){
		Page<PolicyBase> page = new Page<PolicyBase>(pb.getCurPage(),pb.getPageSize());
		page.setAsc(false);
		page.setOrderByField("create_time");
		EntityWrapper<PolicyBase> entityWrapper = new EntityWrapper<>();
		if(base.getTitle()!=null && !base.getTitle().equals("")){
			entityWrapper.like("title",base.getTitle());
		}
		if(base.getCompetent_department_name()!=null && !base.getCompetent_department_name().equals("")){
			entityWrapper.eq("competent_department_name", base.getCompetent_department_name());
		}
		page = policyBaseService.selectPage(page,entityWrapper);
		List<PolicyBase> list = page.getRecords();
		pb = new PageBean(page.getTotal(), page.getPages(), page.getCurrent(), 10, page.getOffsetCurrent(), page.getOffset(),0);
		mv.addObject("pagebean",pb);
		mv.addObject("policyBaselist", list);
		mv.addObject("departlist", departService.selectList(null));
		mv.setViewName("policy/list");
		//sys_policy_base
		return mv;
	}
	
	@GetMapping("/add")
	public ModelAndView add(ModelAndView mv){
		mv.addObject("departlist", departService.selectList(null));
		mv.addObject("policyBase", new PolicyBase());
		mv.setViewName("policy/info");
		return mv;
	}
	
	@PostMapping("/add")
	public ModelAndView add(ModelAndView mv,@Valid PolicyBase policyBase,BindingResult result){
		if(result.hasErrors()){
			mv.setViewName("policy/info");
			mv.addObject("departlist", departService.selectList(null));
			mv.addObject("policyBase",policyBase);
			return mv;
		}
		policyBase.setFist_city(Long.valueOf(policyBase.getFist_city_name().split(",")[1]));
		policyBase.setFist_city_name(policyBase.getFist_city_name().split(",")[0]);
		policyBase.setSecen_city(Long.valueOf(policyBase.getSecen_city_name().split(",")[1]));
		policyBase.setSecen_city_name(policyBase.getSecen_city_name().split(",")[0]);
		policyBase.setThreed_city(Long.valueOf(policyBase.getThreed_city_name().split(",")[1]));
		policyBase.setThreed_city_name(policyBase.getThreed_city_name().split(",")[0]);
		policyBase.setCompetent_department(Long.valueOf(policyBase.getCompetent_department_name().split(",")[1]));
		policyBase.setCompetent_department_name(policyBase.getCompetent_department_name().split(",")[0]);
		policyBase.setDeclaration_departmnt(Long.valueOf(policyBase.getDeclaration_departmnt_name().split(",")[1]));
		policyBase.setDeclaration_departmnt_name(policyBase.getDeclaration_departmnt_name().split(",")[0]);
		policyBase.setAdvisory_department(Long.valueOf(policyBase.getAdvisory_department_name().split(",")[1]));
		Long depart_id = Long.valueOf(policyBase.getAdvisory_department_name().split(",")[1]);
		Depart depart = departService.selectById(depart_id);
		if(null != depart){
			policyBase.setAdvisory_tel(depart.getTel());
		}
		policyBase.setAdvisory_department_name(policyBase.getAdvisory_department_name().split(",")[0]);
		policyBase.setFist_enterprise(policyBase.getFist_enterprise_name().split(",")[1]);
		policyBase.setFist_enterprise_name(policyBase.getFist_enterprise_name().split(",")[0]);
		policyBase.setSecend_enterprise(policyBase.getSecend_enterprise_name().split(",")[1]);
		policyBase.setSecend_enterprise_name(policyBase.getSecend_enterprise_name().split(",")[0]);
		policyBase.setThreed_enterprise(policyBase.getThreed_enterprise_name().split(",")[1]);
		policyBase.setThreed_enterprise_name(policyBase.getThreed_enterprise_name().split(",")[0]);
		policyBase.setFuor_enterprise(policyBase.getFuor_enterprise_name().split(",")[1]);
		policyBase.setFuor_enterprise_name(policyBase.getFuor_enterprise_name().split(",")[0]);
		policyBase.setBeg_time(DateUtil.strToDate(policyBase.getBeg_times()));
		policyBase.setEnd_time(DateUtil.strToDate(policyBase.getEnd_times()));
		policyBaseService.insert(policyBase);
		mv.setViewName("redirect:list");
		return mv;
	}
	
	
	@GetMapping("/update")
	public ModelAndView update(ModelAndView mv,Long id){
		mv.setViewName("policy/info");
		mv.addObject("departlist", departService.selectList(null));
		PolicyBase policyBase = policyBaseService.selectById(id);
		policyBase.setBeg_times(DateUtil.dateToStr(policyBase.getBeg_time(),11));
		policyBase.setEnd_times(DateUtil.dateToStr(policyBase.getEnd_time(), 11));
		mv.addObject("policyBase",policyBase);
		return mv;
	}
	
	@PostMapping("/update")
	public ModelAndView update(ModelAndView mv,@Valid PolicyBase policyBase,BindingResult result){
		if(result.hasErrors()){
			mv.setViewName("policy/info");
			mv.addObject("departlist", departService.selectList(null));
			mv.addObject("policyBase",policyBase);
			return mv;
		}
		policyBase.setFist_city(Long.valueOf(policyBase.getFist_city_name().split(",")[1]));
		policyBase.setFist_city_name(policyBase.getFist_city_name().split(",")[0]);
		policyBase.setSecen_city(Long.valueOf(policyBase.getSecen_city_name().split(",")[1]));
		policyBase.setSecen_city_name(policyBase.getSecen_city_name().split(",")[0]);
		policyBase.setThreed_city(Long.valueOf(policyBase.getThreed_city_name().split(",")[1]));
		policyBase.setThreed_city_name(policyBase.getThreed_city_name().split(",")[0]);
		policyBase.setCompetent_department(Long.valueOf(policyBase.getCompetent_department_name().split(",")[1]));
		policyBase.setCompetent_department_name(policyBase.getCompetent_department_name().split(",")[0]);
		policyBase.setDeclaration_departmnt(Long.valueOf(policyBase.getDeclaration_departmnt_name().split(",")[1]));
		policyBase.setDeclaration_departmnt_name(policyBase.getDeclaration_departmnt_name().split(",")[0]);
		policyBase.setAdvisory_department(Long.valueOf(policyBase.getAdvisory_department_name().split(",")[1]));
		Long depart_id = Long.valueOf(policyBase.getAdvisory_department_name().split(",")[1]);
		Depart depart = departService.selectById(depart_id);
		if(null != depart){
			policyBase.setAdvisory_tel(depart.getTel());
		}
		policyBase.setAdvisory_department_name(policyBase.getAdvisory_department_name().split(",")[0]);
		policyBase.setFist_enterprise(policyBase.getFist_enterprise_name().split(",")[1]);
		policyBase.setFist_enterprise_name(policyBase.getFist_enterprise_name().split(",")[0]);
		policyBase.setSecend_enterprise(policyBase.getSecend_enterprise_name().split(",")[1]);
		policyBase.setSecend_enterprise_name(policyBase.getSecend_enterprise_name().split(",")[0]);
		policyBase.setThreed_enterprise(policyBase.getThreed_enterprise_name().split(",")[1]);
		policyBase.setThreed_enterprise_name(policyBase.getThreed_enterprise_name().split(",")[0]);
		policyBase.setFuor_enterprise(policyBase.getFuor_enterprise_name().split(",")[1]);
		policyBase.setFuor_enterprise_name(policyBase.getFuor_enterprise_name().split(",")[0]);
		policyBase.setBeg_time(DateUtil.strToDate(policyBase.getBeg_times()));
		policyBase.setEnd_time(DateUtil.strToDate(policyBase.getEnd_times()));
		policyBaseService.updateById(policyBase);
		mv.setViewName("redirect:list");
		return mv;
	}
	
	@GetMapping("/delete")
	public ModelAndView delete(ModelAndView mv,Long id){
		mv.setViewName("redirect:list");
		policyBaseService.deleteById(id);
		return mv;
	}
	
	/**
	 * 
	 * @param mv	:更新
	 * @param id
	 * @param sort
	 * @return
	 */
	@GetMapping("/updateSort")
	public ModelAndView updateSort(ModelAndView mv,Long id,Integer sort){
		mv.setViewName("redirect:list");
		if(id == null || sort == null){
			return mv;
		}
		PolicyBase policyBase = new PolicyBase();
		policyBase.setId(id);
		policyBase.setSort(sort);
		policyBaseService.updateById(policyBase);
		return mv;
	}
	
	
	@GetMapping("/sdopOrOpen")
	public ModelAndView sdopOrOpen(ModelAndView mv,Long id,Integer type){
		mv.setViewName("redirect:list");
		if(id == null || type == null){
			return mv;
		}
		PolicyBase policyBase = new PolicyBase();
		policyBase.setId(id);
		policyBase.setFlag_open(type);
		policyBaseService.updateById(policyBase);
		return mv;
	}
	
	
	@GetMapping("/statistics")
	public ModelAndView statistics(ModelAndView mv,String time,PageBean pb){
		mv.setViewName("policy/statics");
		Page<PolicyStatiscesResult> page = new Page<>(pb.getCurPage(), pb.getPageSize());
		String beg_time = "";
		String end_time = "";
		if(time !=null && !time.equals("")){
			 beg_time = time.split("→")[0];
			 end_time = time.split("→")[1];
		}
		mv.addObject("staticsList", policyBaseService.statics(page,beg_time, end_time));
		pb = new PageBean(page.getTotal(), page.getPages(), page.getCurrent(), 10, page.getOffsetCurrent(), page.getOffset(),0);
		mv.addObject("pagebean",pb);
		return mv;
	}
	
	
}

